Skip to content

Conversation

@klausler
Copy link
Contributor

The current parser can fail on "self(x * 2)" by recognizing just "x" as a one-element list of object names and then failing at a higher level because it never reached the right parenthesis. Add lookahead checks and error recovery.

Fixes #135810.

@llvmbot llvmbot added flang Flang issues not falling into any other category openacc flang:semantics flang:parser labels Apr 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 15, 2025

@llvm/pr-subscribers-openacc
@llvm/pr-subscribers-flang-parser

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

The current parser can fail on "self(x * 2)" by recognizing just "x" as a one-element list of object names and then failing at a higher level because it never reached the right parenthesis. Add lookahead checks and error recovery.

Fixes #135810.


Full diff: https://github.com/llvm/llvm-project/pull/135883.diff

3 Files Affected:

  • (modified) flang/lib/Parser/openacc-parsers.cpp (+7-2)
  • (added) flang/test/Semantics/OpenACC/bug135810-1.f90 (+9)
  • (added) flang/test/Semantics/OpenACC/bug135810-2.f90 (+12)
diff --git a/flang/lib/Parser/openacc-parsers.cpp b/flang/lib/Parser/openacc-parsers.cpp
index fb731ee52cbba..5e0a56b1d374c 100644
--- a/flang/lib/Parser/openacc-parsers.cpp
+++ b/flang/lib/Parser/openacc-parsers.cpp
@@ -126,8 +126,13 @@ TYPE_PARSER(construct<AccDefaultClause>(
 // SELF clause is either a simple optional condition for compute construct
 // or a synonym of the HOST clause for the update directive 2.14.4 holding
 // an object list.
-TYPE_PARSER(construct<AccSelfClause>(Parser<AccObjectList>{}) ||
-    construct<AccSelfClause>(scalarLogicalExpr))
+TYPE_PARSER(
+    construct<AccSelfClause>(Parser<AccObjectList>{}) / lookAhead(")"_tok) ||
+    construct<AccSelfClause>(scalarLogicalExpr / lookAhead(")"_tok)) ||
+    construct<AccSelfClause>(
+        recovery(fail<std::optional<ScalarLogicalExpr>>(
+                     "logical expression or object list expected"_err_en_US),
+            SkipTo<')'>{} >> pure<std::optional<ScalarLogicalExpr>>())))
 
 // Modifier for copyin, copyout, cache and create
 TYPE_PARSER(construct<AccDataModifier>(
diff --git a/flang/test/Semantics/OpenACC/bug135810-1.f90 b/flang/test/Semantics/OpenACC/bug135810-1.f90
new file mode 100644
index 0000000000000..c40f4720b4f06
--- /dev/null
+++ b/flang/test/Semantics/OpenACC/bug135810-1.f90
@@ -0,0 +1,9 @@
+! RUN: %python %S/../test_errors.py %s %flang -fopenacc
+integer function square(x)
+    implicit none
+    integer, intent(in) :: x
+!ERROR: Must have LOGICAL type, but is INTEGER(4)
+    !$acc parallel self(x * 2)
+    !$acc end parallel
+    square = x * x
+end function square
diff --git a/flang/test/Semantics/OpenACC/bug135810-2.f90 b/flang/test/Semantics/OpenACC/bug135810-2.f90
new file mode 100644
index 0000000000000..8ed608b0b4cc9
--- /dev/null
+++ b/flang/test/Semantics/OpenACC/bug135810-2.f90
@@ -0,0 +1,12 @@
+! RUN: %python %S/../test_errors.py %s %flang -fopenacc
+integer function square(x)
+    implicit none
+    integer, intent(in) :: x
+!ERROR: logical expression or object list expected
+    !$acc parallel self(,)
+    !$acc end parallel
+!ERROR: logical expression or object list expected
+    !$acc parallel self(.true., )
+    !$acc end parallel
+    square = x * x
+end function square

! RUN: %python %S/../test_errors.py %s %flang -fopenacc
integer function square(x)
implicit none
integer, intent(in) :: x
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about also adding a positive test for something like !$acc parallel self(x * 2 .eq. 4) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks fine to me, but my experience with this codebase is only a few hours old. @razvanlupusoru had some additional knowledge here, and might wish to chime in.

!ERROR: logical expression or object list expected
!$acc parallel self(,)
!$acc end parallel
!ERROR: logical expression or object list expected
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a test that !$acc parallel self(object-list) would cause a diagnostic? (as self only takes an object list when used on an update construct).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's done by semantics, not the parser; but I'll add a test here since there isn't one elsewhere.

The current parser can fail on "self(x * 2)" by recognizing just
"x" as a one-element list of object names and then failing at a
higher level because it never reached the right parenthesis.
Add lookahead checks and error recovery.

Fixes llvm#135810.
Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks Peter for improving this.

@klausler klausler merged commit 6a7044a into llvm:main Apr 18, 2025
11 checks passed
@klausler klausler deleted the bug135810 branch April 18, 2025 19:51
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
The current parser can fail on "self(x * 2)" by recognizing just "x" as
a one-element list of object names and then failing at a higher level
because it never reached the right parenthesis. Add lookahead checks and
error recovery.

Fixes llvm#135810.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:parser flang:semantics flang Flang issues not falling into any other category openacc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[flang][openacc] 'self' clause on compute/combined constructs takes a var-list instead of a condition

5 participants